package com.efast.cafe.portal.service.api.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.api.PackagePublishIndentifierBean;
import com.efast.cafe.portal.bean.api.PackagePublishServiceBean;
import com.efast.cafe.portal.bean.api.PortalApiModuleBean;
import com.efast.cafe.portal.bean.api.PortalApiProjectBean;
import com.efast.cafe.portal.bean.api.PortalApiServiceBean;
import com.efast.cafe.portal.bean.api.PortalApiServiceQueryBean;
import com.efast.cafe.portal.bean.api.PortalApiServiceUpgradeBean;
import com.efast.cafe.portal.bean.api.PortalApiSystemBean;
import com.efast.cafe.portal.dao.api.PackagePublishIndentifierDao;
import com.efast.cafe.portal.dao.api.PackagePublishIndentifierDefDao;
import com.efast.cafe.portal.dao.api.PortalApiInputParamDao;
import com.efast.cafe.portal.dao.api.PortalApiModObjDefDao;
import com.efast.cafe.portal.dao.api.PortalApiModServiceDefDao;
import com.efast.cafe.portal.dao.api.PortalApiModuleDao;
import com.efast.cafe.portal.dao.api.PortalApiOutputParamDao;
import com.efast.cafe.portal.dao.api.PortalApiProSysDefDao;
import com.efast.cafe.portal.dao.api.PortalApiProjectDao;
import com.efast.cafe.portal.dao.api.PortalApiServiceDao;
import com.efast.cafe.portal.dao.api.PortalApiServiceObjDefDao;
import com.efast.cafe.portal.dao.api.PortalApiServiceSampleDao;
import com.efast.cafe.portal.dao.api.PortalApiSysModDefDao;
import com.efast.cafe.portal.dao.api.PortalApiSystemDao;
import com.efast.cafe.portal.entity.api.PortalApiInputParam;
import com.efast.cafe.portal.entity.api.PortalApiObject;
import com.efast.cafe.portal.entity.api.PortalApiOutputParam;
import com.efast.cafe.portal.entity.api.PortalApiService;
import com.efast.cafe.portal.entity.api.PortalApiServiceSample;
import com.efast.cafe.portal.service.api.IPortalApiServiceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * <p>
 * Service接口实现类
 * </p>
 * <p>
 * Table: portal_api_service - 服务定义
 * </p>
 *
 * @since 2017-08-10 02:38:48
 */
@Service
@Transactional
public class PortalApiServiceServiceImpl implements IPortalApiServiceService {

	private PortalApiModServiceDefDao portalApiModServiceDefDao;

	private PortalApiServiceDao portalApiServiceDao;

	private PortalApiInputParamDao portalApiInputParamDao;

	private PortalApiOutputParamDao portalApiOutputParamDao;

	private PortalApiServiceSampleDao portalApiServiceSampleDao;

	private PortalApiServiceObjDefDao portalApiServiceObjDefDao;

	private PackagePublishIndentifierDao packagePublishIndentifierDao;

	private PackagePublishIndentifierDefDao packagePublishIndentifierDefDao;

	private PortalApiSystemDao portalApiSystemDao;

	private PortalApiSysModDefDao portalApiSysModDefDao;

	private PortalApiProSysDefDao portalApiProSysDefDao;

	private PortalApiProjectDao portalApiProjectDao;

	private PortalApiModuleDao portalApiModuleDao;

	private PortalApiSysModDefDao portalApiSysModefDao;

	private PortalApiModObjDefDao portalApiModObjDefDao;

	@Autowired
	public void setPortalApiModServiceDefDao(PortalApiModServiceDefDao portalApiModServiceDefDao) {
		this.portalApiModServiceDefDao = portalApiModServiceDefDao;
	}

	@Autowired
	public void setPortalApiServiceDao(PortalApiServiceDao portalApiServiceDao) {
		this.portalApiServiceDao = portalApiServiceDao;
	}

	@Autowired
	public void setPortalApiInputParamDao(PortalApiInputParamDao portalApiInputParamDao) {
		this.portalApiInputParamDao = portalApiInputParamDao;
	}

	@Autowired
	public void setPortalApiOutputParamDao(PortalApiOutputParamDao portalApiOutputParamDao) {
		this.portalApiOutputParamDao = portalApiOutputParamDao;
	}

	@Autowired
	public void setPortalApiServiceSampleDao(PortalApiServiceSampleDao portalApiServiceSampleDao) {
		this.portalApiServiceSampleDao = portalApiServiceSampleDao;
	}

	@Autowired
	public void setPortalApiServiceObjDefDao(PortalApiServiceObjDefDao portalApiServiceObjDefDao) {
		this.portalApiServiceObjDefDao = portalApiServiceObjDefDao;
	}

	@Autowired
	public void setPackagePublishIndentifierDao(PackagePublishIndentifierDao packagePublishIndentifierDao) {
		this.packagePublishIndentifierDao = packagePublishIndentifierDao;
	}

	@Autowired
	public void setPackagePublishIndentifierDefDao(PackagePublishIndentifierDefDao packagePublishIndentifierDefDao) {
		this.packagePublishIndentifierDefDao = packagePublishIndentifierDefDao;
	}

	@Autowired
	public void setPortalApiSystemDao(PortalApiSystemDao portalApiSystemDao) {
		this.portalApiSystemDao = portalApiSystemDao;
	}

	@Autowired
	public void setPortalApiSysModDefDao(PortalApiSysModDefDao portalApiSysModDefDao) {
		this.portalApiSysModDefDao = portalApiSysModDefDao;
	}

	@Autowired
	public void setPortalApiProSysDefDao(PortalApiProSysDefDao portalApiProSysDefDao) {
		this.portalApiProSysDefDao = portalApiProSysDefDao;
	}

	@Autowired
	public void setPortalApiProjectDao(PortalApiProjectDao portalApiProjectDao) {
		this.portalApiProjectDao = portalApiProjectDao;
	}

	@Autowired
	public void setPortalApiModuleDao(PortalApiModuleDao portalApiModuleDao) {
		this.portalApiModuleDao = portalApiModuleDao;
	}

	@Autowired
	public void setPortalApiSysModefDao(PortalApiSysModDefDao portalApiSysModefDao) {
		this.portalApiSysModefDao = portalApiSysModefDao;
	}

	@Autowired
	public void setPortalApiModObjDefDao(PortalApiModObjDefDao portalApiModObjDefDao) {
		this.portalApiModObjDefDao = portalApiModObjDefDao;
	}

	/**
	 * 
	 * <p>
	 * Title: insertApiService
	 * </p>
	 * 
	 * @param portalApiServiceBean
	 * @Date 2017年8月11日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#insertApiService(com.efast.cafe.portal.bean.api.PortalApiServiceBean)
	 */
	@Override
	public void insertApiService(PortalApiServiceBean portalApiServiceBean) {
		apiServiceBeanCheck(portalApiServiceBean);
	//portalApiServiceBean.setServiceName("{\"en_US\": \"test ser\", \"zh_CN\": \"测试ser\"}");
		String user = SpringWebUtils.getRequestUser();
		Integer moduleId = portalApiServiceBean.getModuleId();
		if (moduleId == null) {
			throw new ServiceException("请选择模块！");
		}
		if (portalApiServiceBean.getId() == null) {
			// 产生服务唯一标识
			portalApiServiceBean.setUuid(UUID.randomUUID().toString());
			portalApiServiceBean.setCreateUser(user);
			portalApiServiceBean.setStatus((byte) 2);
		}
		// 产生服务基本信息实例
		portalApiServiceDao.insertApiService(portalApiServiceBean);
		Integer serviceId = portalApiServiceBean.getId();
		portalApiModServiceDefDao.insertApiModuleServiceDef(moduleId, serviceId, user);
		if (serviceId != null) {
			// 产生服务参数信息
			insertServcieParam(portalApiServiceBean, user, serviceId);
		}

	}

	/**
	 * 
	 * <p>
	 * Title: updateApiService
	 * </p>
	 * 
	 * @param portalApiServiceBean
	 * @Date 2017年8月11日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#updateApiService(com.efast.cafe.portal.bean.api.PortalApiServiceBean)
	 */
	@Override
	public void updateApiService(PortalApiServiceBean portalApiServiceBean) {
		// 检查服务信息的有效性
		apiServiceBeanCheck(portalApiServiceBean);
		// 测试数据
		String user = SpringWebUtils.getRequestUser();
		portalApiServiceBean.setCreateUser(user);
		// 修改服务基本信息实例
		portalApiServiceDao.updateApiService(portalApiServiceBean);
		Integer serviceId = portalApiServiceBean.getId();
		deleteServiceParam(serviceId);
		// 插入服务新的参数
		insertServcieParam(portalApiServiceBean, user, serviceId);
	}

	/**
	 * 
	 * <p>
	 * Title: deleteApiService
	 * </p>
	 * 
	 * @param id
	 * @param user
	 * @Date 2017年8月11日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#deleteApiService(java.lang.Integer,
	 *      java.lang.String)
	 */
	@Override
	public void deleteApiService(Integer id) {
		String user = SpringWebUtils.getRequestUser();
		// 删除服务请求参数列表
		portalApiServiceDao.deleteApiService(id, user);
		// 删除服务请求参数列表
		deleteServiceParam(id);
	}

	/**
	 * 
	 * <p>
	 * Title: QueryPortalApiServiceDetailById
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @Date 2017年8月12日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#QueryPortalApiServiceDetailById(java.lang.Integer)
	 */
	@Override
	public PortalApiServiceBean QueryPortalApiServiceDetailById(Integer id) {
		if (id != null) {
			return portalApiServiceDao.queryPortalApiServiceDetail(id);
		}
		return null;

	}

	/**
	 * 批量发布服务
	 * <p>
	 * Title: publishPortalApiServiceList
	 * </p>
	 * 
	 * @param packagePublishServiceBean
	 * @param packagePublishIndentifierBean
	 * @Date 2017年8月14日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#publishPortalApiServiceList(com.efast.cafe.portal.bean.api.PackagePublishServiceBean)
	 */
	@Override
	public void publishPortalApiServiceList(PackagePublishServiceBean packagePublishServiceBean,
			PackagePublishIndentifierBean packagePublishIndentifierBean) {
		List<PortalApiServiceBean> serviceList = packagePublishServiceBean.getServiceList();

//		for (PortalApiServiceBean portalApiServiceBean : serviceList) {
//			if (apiServiceVersionIsExist(portalApiServiceBean)) {
//				throw new ServiceException(portalApiServiceBean.getVersion() + "版本重复！");
//			}
//		}

		if (serviceList != null && !serviceList.isEmpty()) {
			String user = SpringWebUtils.getRequestUser();
			// 新增服务的发布版本
			portalApiServiceDao.publishPortalApiServiceList(serviceList, user);
			// 保存发布服务列表中服务的id
			List<Integer> refIds = new ArrayList<Integer>();
			for (PortalApiService service : serviceList) {
				refIds.add(service.getId());
			}
			Integer moduleId = packagePublishServiceBean.getModuleId();
			// 新增发布服务与模块关系
			portalApiModServiceDefDao.insertApiModuleServiceListDefs(moduleId, refIds, user);
			// 将服务的参数复制到发布的版本中
			copyServiceListParamsTo(refIds, user);
			// 新增打包发布标识
			packagePublishIndentifierBean.setCreateUser(SpringWebUtils.getRequestUser());
			packagePublishIndentifierDao.insertPackagePublishIndentifier(packagePublishIndentifierBean);
			if (!refIds.isEmpty()) {
				// 新增打包标识符与服务的关系
				packagePublishIndentifierDefDao.insertPackagePublishIndentifierDefs(refIds,
						packagePublishIndentifierBean.getId(), user);
			}
		}
	}

	/**
	 * 升级服务
	 * <p>
	 * Title: upgradePortalApiService
	 * </p>
	 * 
	 * @param portalApiServiceUpgradeBean
	 * @Date 2017年8月15日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#upgradePortalApiService(com.efast.cafe.portal.bean.api.PortalApiServiceUpgradeBean)
	 */
	@Override
	public void upgradePortalApiService(PortalApiServiceUpgradeBean portalApiServiceUpgradeBean) {

		List<PortalApiServiceBean> serviceList = portalApiServiceUpgradeBean.getServiceList();
		if (serviceList.isEmpty()) {
			throw new ServiceException("请选择要升级的服务！");
		}
		PortalApiProjectBean project = portalApiServiceUpgradeBean.getProject();
		PortalApiModuleBean module = portalApiServiceUpgradeBean.getModule();
		PortalApiSystemBean system = portalApiServiceUpgradeBean.getSystem();
		Integer projectId = null, systemId = null, moduleId = null;
		if (system != null) {
			projectId = system.getProjectId();
		}
		if (module != null) {
			systemId = module.getSystemId();
		}
		moduleId = serviceList.get(0).getModuleId();
		String user = SpringWebUtils.getRequestUser();
		if (project != null) {
			// 升级项目,并返回最新项目的id
			projectId = upgradeProject(project, user);
		}
		if (system != null) {
			// 升级系统,并返回最新系统的id
			systemId = upgradeSystem(system, projectId, user);
		}
		if (module != null) {
			// 升级模块,并返回最新模块的id
			moduleId = upgradeModule(module, systemId, user);
		}
		// 升级服务
		upgradeService(serviceList, moduleId, user);

	}

	/**
	 * 删除服务参数
	 * 
	 * @param id
	 *            服务id
	 * @return void
	 * @Author Administrator
	 * @Date 2017年8月26日
	 */
	private void deleteServiceParam(Integer id) {
		portalApiInputParamDao.deleteApiInputParams(id);
		// 删除服务响应参数列表
		portalApiOutputParamDao.deleteApiOutputParams(id);
		// 删除服务对象关系列表
		portalApiServiceObjDefDao.deleteApiServiceObjDef(id);
		// 删除服务展示示例列表
		portalApiServiceSampleDao.deleteApiServiceSample(id);
	}

	/**
	 * 升级服务
	 * 
	 * @param serviceList
	 *            待升级的服务列表
	 * @param moduleId
	 *            服务所在的模块id
	 * @param user
	 * @return void
	 * @Author Administrator
	 * @Date 2017年8月25日
	 */
	private void upgradeService(List<PortalApiServiceBean> serviceList, Integer moduleId, String user) {
		// 产生升级后的后的服务
		portalApiServiceDao.copyPortalApiService(serviceList, user);
		List<Integer> refIds = new ArrayList<Integer>();
		for (PortalApiServiceBean service : serviceList) {
			refIds.add(service.getId());
		}
		// 新增升版的服务与模块关系
		portalApiModServiceDefDao.insertApiModuleServiceListDefs(moduleId, refIds, user);
		// 复制求情响应示例对象关系
		copyServiceListParamsTo(refIds, user);
	}

	/**
	 * 升级模块并返回升级后模块id
	 * 
	 * @param module
	 * @param systemId
	 * @param user
	 * @return Integer
	 * @Author Administrator
	 * @Date 2017年8月15日
	 */
	private Integer upgradeModule(PortalApiModuleBean module, Integer systemId, String user) {
		if (portalApiModuleDao.moduleVersionIsExistWhenInsert(module) != 0) {
			throw new ServiceException("存在相同版本，请修改版本");
		}
		module.setStatus((byte) 2);
		module.setParentId(module.getId());
		module.setParentLastTime(module.getLastUpdateTime());
		// 升级模块的时候，相当于复制模块
		portalApiModuleDao.insertApiModule(module);
		Integer newModuleId = module.getId();
		// 复制模块跟系统的关系
		portalApiSysModefDao.insertApiSysModDef(systemId, newModuleId, user, 1);
		// 复制模块跟服务的关系
		portalApiModServiceDefDao.upgradeApiModuleServiceDef(newModuleId, module.getParentId(), user);
		// 复制模块跟对象的关系
		portalApiModObjDefDao.upgradeApiModObjDef(newModuleId, module.getParentId(), user);
		return newModuleId;
	}

	/**
	 * 升级系统，并返回升级后的系统id
	 * 
	 * @param system
	 * @param projectId
	 * @param user
	 * @return void
	 * @Author Administrator
	 * @Date 2017年8月15日
	 */
	private Integer upgradeSystem(PortalApiSystemBean system, Integer projectId, String user) {
		Integer fromId = system.getId();
		portalApiSystemDao.copyApiSystem(system);
		Integer toId = system.getId();
		portalApiProSysDefDao.insertApiProSysDef(projectId, toId, 0, user);
		// 新返回的的id
		portalApiSysModDefDao.copyApiSysModDefListTo(fromId, toId, user);
		return toId;
	}

	/**
	 * 升级项目并返回升级后的项目id
	 * 
	 * @param project
	 * @param user
	 * @return int
	 * @Author Administrator
	 * @Date 2017年8月15日
	 */
	private Integer upgradeProject(PortalApiProjectBean project, String user) {
		if (portalApiProjectDao.projectVersionInsert(project) > 0) {
			throw new ServiceException("版本重复，请重新输入版本");
		}
		project.setParentId(project.getId());
		project.setStatus((byte) 2);
		project.setParentLastTime(project.getLastUpdateTime());
		// 发布项目，相当于复制项目
		portalApiProjectDao.insertApiProject(project);
		Integer newProjectId = project.getId();
		portalApiProSysDefDao.upgradeApiProSysDef(newProjectId, project.getParentId(), user);
		return newProjectId;
	}

	/**
	 * 将服务的参数复制到发布的版本中
	 * 
	 * @param user
	 * @param refIds
	 * @return void
	 * @Author Administrator
	 * @Date 2017年8月14日
	 */
	private void copyServiceListParamsTo(List<Integer> refIds, String user) {
		// 复制请求参数
		portalApiInputParamDao.copyApiInputParams(refIds, user);
		// 复制响应参数
		portalApiOutputParamDao.copyApiOutputParams(refIds, user);
		// 复制示例列表
		portalApiServiceSampleDao.copyApiSamples(refIds, user);
		// 复制对象关系
		portalApiServiceObjDefDao.copyApiObjs(refIds, user);
	}

	/**
	 * 查询所有系统,返回系统最新版本
	 * <p>
	 * Title: queryPortalApiServiceList
	 * </p>
	 * 
	 * @param queryBean
	 * @return
	 * @Date 2017年8月22日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#queryPortalApiServiceList(com.efast.cafe.portal.bean.api.PortalApiServiceQueryBean)
	 */
	@Override
	public PortalApiServiceQueryBean queryPortalApiServiceList(PortalApiServiceQueryBean queryBean) {
		queryBean.setResultList(portalApiServiceDao.queryPortalApiServiceList(queryBean));
		queryBean.setTotalRows(portalApiServiceDao.queryPortalApiServiceListCount(queryBean));
		return queryBean;
	}

	/**
	 * 查询系统所有版本
	 * <p>
	 * Title: queryPortalApiServiceVersions
	 * </p>
	 * 
	 * @param queryBean
	 * @return
	 * @Date 2017年8月22日
	 * @see com.efast.cafe.portal.remote.api.PortalApiServiceService#queryPortalApiServiceVersions(com.efast.cafe.portal.bean.api.PortalApiServiceQueryBean)
	 */
	@Override
	public PortalApiServiceQueryBean queryPortalApiServiceVersions(PortalApiServiceQueryBean queryBean) {
		Integer moduleId = queryBean.getModuleId();
		Integer serviceId = queryBean.getServiceId();
		Integer status = queryBean.getStatus();
		if (moduleId != null && serviceId != null) {
			queryBean.setResultList(portalApiServiceDao.queryPortalApiServiceVersions(moduleId, serviceId, status));
		}
		return queryBean;
	}

	@Override
	public boolean apiServiceVersionIsExist(PortalApiServiceBean portalApiServiceBean) {
		if (portalApiServiceDao.apiServiceVersionsIsExist(portalApiServiceBean) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @param portalApiServiceBean
	 * @param user
	 * @param serviceId
	 * @return void
	 * @Author Administrator
	 * @Date 2017年8月26日
	 */
	private void insertServcieParam(PortalApiServiceBean portalApiServiceBean, String user, Integer serviceId) {
		// 解析请求定义
		String reqDefine = portalApiServiceBean.getReqDefine();
		if (reqDefine != null && !reqDefine.isEmpty()) {
			portalApiServiceBean.setListRequestParam(paraseReqDefine(reqDefine));
		}
		// 解析响应定义
		String resDefine = portalApiServiceBean.getResDefine();
		if (resDefine != null && !resDefine.isEmpty()) {
			portalApiServiceBean.setListResponseParam(paraseResDefine(resDefine));
		}
		List<PortalApiInputParam> portalApiInputParamList = portalApiServiceBean.getListRequestParam();
		if (portalApiInputParamList != null && !portalApiInputParamList.isEmpty()) {
			// 保存服务请求参数列表
			portalApiInputParamDao.insertApiInputParamList(portalApiInputParamList, serviceId, user);
		}

		List<PortalApiOutputParam> portalApiOutputParamList = portalApiServiceBean.getListResponseParam();
		if (portalApiOutputParamList != null && !portalApiOutputParamList.isEmpty()) {
			// 保存服务响应参数列表
			portalApiOutputParamDao.insertApiOutputParamList(portalApiOutputParamList, serviceId, user);
		}
		 List<PortalApiObject> objList = portalApiServiceBean.getObjList();
		if (objList != null && !objList.isEmpty()) {
			// 保存服务对象关系列表
			portalApiServiceObjDefDao.insertApiServiceObjDef(objList, serviceId, user);
		}
		List<PortalApiServiceSample> sampleList = portalApiServiceBean.getListUriSample();
		if (sampleList != null && !sampleList.isEmpty()) {
			if (sampleList.size() == 1) {
				sampleList.get(0).setIsDefault((byte) 1);
			}
			// 保存服务展示示例列表
			portalApiServiceSampleDao.insertApiServiceSample(sampleList, serviceId, user);
		}
	}

	/**
	 * 服务信息的校验
	 * 
	 * @param portalApiServiceBean
	 *            void
	 * @Author Administrator
	 * @Date 2017年8月24日
	 */
	private void apiServiceBeanCheck(PortalApiServiceBean portalApiServiceBean) {
		// 服务路径验证
		String reqDefine = portalApiServiceBean.getReqDefine();
		String resDefine = portalApiServiceBean.getResDefine();
		String indentifier=portalApiServiceBean.getIndentifier();
		if(indentifier==null){
			throw new ServiceException("标识符不能为空！");
		}
		if(portalApiServiceDao.apiServiceIndentifierIsExist(portalApiServiceBean)>0){
			throw new ServiceException("标识符已存在！");
		}
		if (reqDefine != null) {
			reqDefine = reqDefine.trim();
			portalApiServiceBean.setReqDefine(reqDefine);
		}
		if (resDefine != null) {
			resDefine = resDefine.trim();
			portalApiServiceBean.setResDefine(resDefine);
		}
	}

	/**
	 * 将请求定义解析为json
	 * 
	 * @param reqDefine
	 * @return List<PortalApiInputParam>
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private List<PortalApiInputParam> paraseReqDefine(String reqDefine) {
		JSONObject jsonObj = (JSONObject) JSONObject.parse(reqDefine);
		JSONArray jsonArr = jsonObj.getJSONArray("paramList");
		List<PortalApiInputParam> listRequestParam = new ArrayList<PortalApiInputParam>();
		for (int i = 0; i < jsonArr.size(); i++) {
			JSONObject obj = (JSONObject) jsonArr.get(i);
			int level = 0;
			PortalApiInputParam param = paraseJsonToInputParam(obj, 0);
			listRequestParam.add(param);
			listRequestParam = paraseJsonToInputParams(obj, level, listRequestParam);
		}
		return listRequestParam;
	}

	/**
	 * 从json递归中解析出请求参数json
	 * 
	 * @param jsonObj
	 * @param level
	 * @param listRequestParam
	 * @return List<PortalApiInputParam>
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private List<PortalApiInputParam> paraseJsonToInputParams(JSONObject jsonObj, int level,
			List<PortalApiInputParam> listRequestParam) {
		JSONArray jsonArr = jsonObj.getJSONArray("children");
		if (jsonArr == null) {
			return listRequestParam;
		}
		level++;
		for (int i = 0; i < jsonArr.size(); i++) {
			JSONObject obj = (JSONObject) jsonArr.get(i);
			PortalApiInputParam param = paraseJsonToInputParam(obj, level);
			listRequestParam.add(param);
			paraseJsonToInputParams(obj, level, listRequestParam);
		}
		return listRequestParam;
	}

	/**
	 * 从json中解析请求参数
	 * 
	 * @param jsonObj
	 * @param level
	 * @return PortalApiInputParam
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private PortalApiInputParam paraseJsonToInputParam(JSONObject jsonObj, int level) {
		PortalApiInputParam param = new PortalApiInputParam();
		String paramName = jsonObj.getString("paramName");
		Byte paramDataType = jsonObj.getByte("paramDataType");
		Byte paramMatchType = jsonObj.getByte("paramMatchType");
		Byte required = jsonObj.getByte("required");
		String paramDesc = jsonObj.getString("paramDesc");
		paramName = getPrefixName(paramName, "children", level);
		param.setParamName(paramName);
		param.setParamDataType(paramDataType);
		param.setParamMatchType(paramMatchType);
		param.setParamDesc(paramDesc);
		param.setRequired(required);
		return param;
	}

	/**
	 * 将请求响应解析为json
	 * 
	 * @param resDefine
	 * @return List<PortalApiOutputParam>
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private List<PortalApiOutputParam> paraseResDefine(String resDefine) {
		JSONObject jsonObj = (JSONObject) JSONObject.parse(resDefine);
		JSONArray jsonArr = jsonObj.getJSONArray("paramList");
		List<PortalApiOutputParam> listResponseParam = new ArrayList<PortalApiOutputParam>();
		for (int i = 0; i < jsonArr.size(); i++) {
			JSONObject obj = (JSONObject) jsonArr.get(i);
			int level = 0;
			PortalApiOutputParam param = paraseJsonToOutputParam(obj, 0);
			listResponseParam.add(param);
			listResponseParam = paraseJsonToOutputParams(obj, level, listResponseParam);
		}
		return listResponseParam;
	}

	/**
	 * 从json递归中解析出每个响应参数json
	 * 
	 * @param jsonObj
	 * @param level
	 * @param listResponseParam
	 * @return List<PortalApiOutputParam>
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private List<PortalApiOutputParam> paraseJsonToOutputParams(JSONObject jsonObj, int level,
			List<PortalApiOutputParam> listResponseParam) {
		JSONArray jsonArr = jsonObj.getJSONArray("children");
		if (jsonArr == null) {
			return listResponseParam;
		}
		level++;
		for (int i = 0; i < jsonArr.size(); i++) {
			JSONObject obj = (JSONObject) jsonArr.get(i);
			PortalApiOutputParam param = paraseJsonToOutputParam(obj, level);
			listResponseParam.add(param);
			paraseJsonToOutputParams(obj, level, listResponseParam);
		}
		return listResponseParam;
	}

	/**
	 * 从响应参数json解析出每个响应参数
	 * 
	 * @param jsonObj
	 * @param level
	 * @return PortalApiOutputParam
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private PortalApiOutputParam paraseJsonToOutputParam(JSONObject jsonObj, int level) {
		PortalApiOutputParam param = new PortalApiOutputParam();
		String paramName = jsonObj.getString("paramName");
		Byte paramDataType = jsonObj.getByte("paramDataType");
		Byte required = jsonObj.getByte("required");
		String paramDesc = jsonObj.getString("paramDesc");
		paramName = getPrefixName(paramName, "children", level);
		param.setParamName(paramName);
		param.setParamDataType(paramDataType);
		param.setParamDesc(paramDesc);
		param.setRequired(required);
		return param;
	}

	private String getPrefixName(String name, String prefix, int level) {
		for (int i = 0; i < level; i++) {
			name = prefix + "." + name;
		}
		return name;
	}

	@Override
	public List<PortalApiInputParam> parseInputparams(String reqDefine) {
		if (reqDefine != null && !reqDefine.isEmpty()) {
			return paraseReqDefine(reqDefine);
		}
		return null;
	}

	@Override
	public List<PortalApiOutputParam> parseOutputparams(String resDefine) {
		if (resDefine != null && !resDefine.isEmpty()) {
			return paraseResDefine(resDefine);
		}
		return null;
	}

	@Override
	public List<PortalApiServiceBean> queryServiceByUri(String serviceUri,String indentifier) {
		// TODO Auto-generated method stub
		return portalApiServiceDao.queryServiceByUri(serviceUri,ConfigUtil.getValue("system.ver"),ConfigUtil.getValue("system.indentifier"),indentifier);
	}
}